import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return new Promise((resolve) => {
    resolve({
      id: 0,
      password: '123456',
      realName: 'Vben',
      roles: ['super'],
      username: 'vben',
      accessToken:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MCwicGFzc3dvcmQiOiIxMjM0NTYiLCJyZWFsTmFtZSI6IlZiZW4iLCJyb2xlcyI6WyJzdXBlciJdLCJ1c2VybmFtZSI6InZiZW4iLCJpYXQiOjE3NTAwNjEzMzUsImV4cCI6MTc1MDY2NjEzNX0.6rYn27UyWl7eonET2tZcHhWJF5pVXNTDU3UkhbaG3Tc',
    });
  });
  return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return new Promise((resolve) => {
    resolve({ data: '' });
  });
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return new Promise((resolve) => {
    resolve({
      data: ['AC_100100', 'AC_100110', 'AC_100120', 'AC_100010'],
    });
  });
  return requestClient.get<string[]>('/auth/codes');
}
